'use strict';

/**
 * Config for the router
 */
angular.module('app')
    .run(
        ['$rootScope', '$state', '$stateParams',
            function ($rootScope, $state, $stateParams) {
                $rootScope.$state = $state;
                $rootScope.$stateParams = $stateParams;
            }
        ]
    )
    .config(
        ['$stateProvider', '$urlRouterProvider', 'JQ_CONFIG', 'MODULE_CONFIG',
            function ($stateProvider, $urlRouterProvider, JQ_CONFIG, MODULE_CONFIG) {
                var layout = "/tpl/app.html";
                if(typeof window.appInfo.user === "object"){
                    $urlRouterProvider
                        .otherwise('/app/dashboard');
                } else {
                    $urlRouterProvider
                        .otherwise('/access/signin');
                }

                $stateProvider
                    .state('access', {
                        abstract: true,
                        url: '/access',
                        v_public: true,
                        templateUrl: '/tpl/page/account/access.html'
                    })
                    .state('access.signin', {
                        url: '/signin',
                        templateUrl: '/tpl/page/account/signin.html',
                        resolve: load(['/libs/particles/particles.min.js', '/js/controllers/account/signin.js'])
                    })
                    .state('access.info', {
                        url: '/info',
                        templateUrl: '/tpl/page/account/info.html',
                        resolve: load(['/libs/particles/particles.min.js', '/js/controllers/account/info.js'])
                    })
                    .state('access.forgotpwd', {
                        url: '/forgotpwd',
                        templateUrl: '/tpl/page/forgotpwd.html'
                    })

                    .state('app', {
                        abstract: true,
                        url: '/app',
                        templateUrl: layout
                    })
                    .state('app.dashboard', {
                        url: '/dashboard',
                        templateUrl: '/tpl/page/dashboard.html',
                        resolve: load(['toaster', '/js/controllers/dashboard.js'])
                    })
                    .state('app.useRecord', {
                        url: '/useRecord',
                        templateUrl: '/tpl/page/use_log/index.html',
                        resolve: load(['toaster', '/js/controllers/use_log/index.js'])
                    })
                    .state('app.buyRecord', {
                        url: '/buyRecord',
                        templateUrl: '/tpl/page/buy_log/index.html',
                        resolve: load(['/js/controllers/buy_log/index.js'])
                    })
                    .state('app.payment', {
                        url: '/payment',
                        templateUrl: '/tpl/page/payment/index.html',
                        resolve: load(['/js/controllers/payment/index.js'])
                    })
                    .state('app.cache', {
                        url: '/cache',
                        templateUrl: '/tpl/page/cache/index.html',
                        resolve: load(['toaster', '/js/controllers/cache/index.js'])
                    })
                    .state('app.regular', {
                        url: '/regular',
                        templateUrl: '/tpl/page/regular/index.html',
                        resolve: load(['toaster', '/js/controllers/regular/index.js'])
                    })
                    .state('app.introduction', {
                        url: '/introduction',
                        templateUrl: '/tpl/page/illustration/introduction.html',
                        resolve: load(['/js/controllers/illustration/index.js'])
                    })
                    .state('app.useMethod', {
                        url: '/useMethod',
                        templateUrl: '/tpl/page/illustration/useMethod.html',
                        resolve: load(['/js/controllers/illustration/index.js'])
                    })
                    .state('app.returnMsg', {
                        url: '/returnMsg',
                        templateUrl: '/tpl/page/illustration/returnMsg.html',
                        resolve: load(['/js/controllers/illustration/index.js'])
                    })
                    .state('app.ca', {
                        url: '/ca',
                        templateUrl: '/tpl/page/illustration/ca.html',
                        resolve: load(['/js/controllers/illustration/index.js'])
                    })
                    .state('app.exp', {
                        url: '/exp',
                        templateUrl: '/tpl/page/illustration/exp.html',
                        resolve: load(['/js/controllers/illustration/index.js'])
                    });

                function load(srcs, callback) {
                    return {
                        deps: ['$ocLazyLoad', '$q',
                            function ($ocLazyLoad, $q) {
                                var deferred = $q.defer();
                                var promise = false;
                                srcs = angular.isArray(srcs) ? srcs : srcs.split(/\s+/);
                                if (!promise) {
                                    promise = deferred.promise;
                                }
                                angular.forEach(srcs, function (src) {
                                    promise = promise.then(function () {
                                        let name;
                                        if (JQ_CONFIG[src]) {
                                            return $ocLazyLoad.load(JQ_CONFIG[src]);
                                        }
                                        angular.forEach(MODULE_CONFIG, function (module) {
                                            if (module.name == src) {
                                                name = module.name;
                                            } else {
                                                name = src;
                                            }
                                        });
                                        return $ocLazyLoad.load(name);
                                    });
                                });
                                deferred.resolve();
                                return callback ? promise.then(function () {
                                    return callback();
                                }) : promise;
                            }]
                    }
                }
            }
        ]
    );
